package com.weilele.mvvm.widget

import android.content.Context
import android.util.AttributeSet
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.weilele.mvvm.utils.vLaout.MvvmHolder
import com.weilele.mvvm.utils.vLaout.MvvmRcvAdapter
import com.weilele.mvvm.utils.vLaout.mvvmRcvAdapter

open class BaseRecyclerView : RecyclerView {
    constructor(context: Context) : super(context)
    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs)
    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(
            context,
            attrs,
            defStyleAttr
    )

    /**
     * holder必须这样重写构造函数
     * constructor(parent: ViewGroup, viewType: Int) :
     *      super(parent, viewType, R.layout.rcv_item_welcome)
     *
     * constructor(parent: ViewGroup, viewType: Int, fragment: Fragment?) :
     *         super(parent, viewType, R.layout.rcv_item_welcome, fragment)
     *
     *  或者自己设置拦截
     */
    inline fun <DataType, reified Holder : MvvmHolder<DataType, *>> setDatas(datas: MutableList<DataType>?): MvvmRcvAdapter<DataType, Holder> {
        val rcvAdapter = mvvmRcvAdapter(Holder::class.java, datas)
        if (this is BaseVLayoutView) {
            addAdapter(rcvAdapter)
        } else {
            adapter = rcvAdapter
            if (layoutManager == null) {
                layoutManager = LinearLayoutManager(context)
            }
        }
        return rcvAdapter
    }
}